"""
主要是应用在实时检查止盈止损
相对于直接在持仓流处理的侧重点：
1）侧重价格维度的止盈止损
2）侧重实时性，预计算性，回测时通过high,low实现判断，先计算止损，再止盈
3）执行信号需要再ss_signals中处理，这里只计算止盈止损的指标

参数：止盈止损线
方法：按百分比，按tick，按算子变动字段
输出：1-止盈，-1止损，None,0 其它不动——策略信号使用
"""
import math
from mohe.core import obj as objtool
class pos_hold_pl:
    def __init__(self, stop0,stop1):
        self.stop0=stop0
        self.stop1=stop1
    def pos_hold_plr_bytick(self,data, b, pos0, fund, ss):
        bs=pos0["bs"]
        pos_price = pos0["costprice"]
        stop_v=0
        obj=b["obj"]
        tk=b["timekey"]
        if bs=="buy":
            #检查止损
            # plr=(b["low"]-pos_price)/pos_price
            price=pos_price*(1+self.stop0)
            price=objtool.get_price(price,obj=b["obj"],bs="sell")
            if b["low"]<price:
                stop_v=-1
                msg="止损："+str(self.stop0)+","+str(price)+","+str(b["low"])
                ss.pos.closesell(timekey=tk, obj=obj, price=price,posrate=1, msg=msg)
            if stop_v==0:
                #检查止盈
                # plr=(b["high"]-pos_price)/pos_price
                price = pos_price * (1 + self.stop1)
                price = objtool.get_price(price, obj=b["obj"], bs="buy")
                if b["high"] > price:
                    stop_v = 1
                    msg = "止赢：" + str(self.stop1) + "," + str(price) + "," + str(b["high"])
                    ss.pos.closesell(timekey=tk, obj=obj, price=price, posrate=1, msg=msg)
        else:
            # 检查止损
            # plr=(b["low"]-pos_price)/pos_price
            price = pos_price * (1 - self.stop0)
            price = objtool.get_price(price, obj=b["obj"], bs="buy")
            if b["high"] > price:
                stop_v = -1
                msg = "止损：" + str(self.stop0) + "," + str(price) + "," + str(b["high"])
                ss.pos.closebuy(timekey=tk, obj=obj, price=price, posrate=1, msg=msg)
            if stop_v == 0:
                # 检查止盈
                # plr=(b["high"]-pos_price)/pos_price
                price = pos_price * (1 - self.stop1)
                price = objtool.get_price(price, obj=b["obj"], bs="sell")
                if b["low"] < price:
                    stop_v = 1
                    msg = "止赢：" + str(self.stop1) + "," + str(price) + "," + str(b["high"])
                    ss.pos.closebuy(timekey=tk, obj=obj, price=price, posrate=1, msg=msg)
    def pos_hold_pl_bytick(data, b, pos0, fund, ss):
        bs = pos0["bs"]
        pos_price = pos0["costprice"]
        stop_v = 0
        obj = b["obj"]
        tk = b["timekey"]
        if bs == "buy":
            # 检查止损
            # plr=(b["low"]-pos_price)/pos_price
            price = pos_price * + self.stop0
            price = objtool.get_price(price, obj=b["obj"], bs="sell")
            if b["low"] < price:
                stop_v = -1
                msg = "止损：" + str(self.stop0) + "," + str(price) + "," + str(b["low"])
                ss.pos.closesell(timekey=tk, obj=obj, price=price, posrate=1, msg=msg)
            if stop_v == 0:
                # 检查止盈
                # plr=(b["high"]-pos_price)/pos_price
                price = pos_price  + self.stop1
                price = objtool.get_price(price, obj=b["obj"], bs="buy")
                if b["high"] >= price:
                    stop_v = 1
                    msg = "止赢：" + str(self.stop1) + "," + str(price) + "," + str(b["high"])
                    ss.pos.closesell(timekey=tk, obj=obj, price=price, posrate=1, msg=msg)
        else:
            # 检查止损
            # plr=(b["low"]-pos_price)/pos_price
            price = pos_price  - self.stop0
            price = objtool.get_price(price, obj=b["obj"], bs="buy")
            if b["high"] > price:
                stop_v = -1
                msg = "止损：" + str(self.stop0) + "," + str(price) + "," + str(b["high"])
                ss.pos.closebuy(timekey=tk, obj=obj, price=price, posrate=1, msg=msg)
            if stop_v == 0:
                # 检查止盈
                # plr=(b["high"]-pos_price)/pos_price
                price = pos_price  - self.stop1
                price = objtool.get_price(price, obj=b["obj"], bs="sell")
                if b["low"] < price:
                    stop_v = 1
                    msg = "止赢：" + str(self.stop1) + "," + str(price) + "," + str(b["high"])
                    ss.pos.closebuy(timekey=tk, obj=obj, price=price, posrate=1, msg=msg)